programming4us
           
 
 
SQL Server

Administering SQL Server 2008 with PowerShell : Step-By-Step Examples (part 1) - General Tasks & Scheduling Scripts

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/20/2011 11:35:08 AM
The following sections provide examples of using PowerShell both for general tasks and for SQL Server 2008–specific tasks. We expand on some of the basic concepts introduced earlier with SQL Server 2008–specific examples.

General Tasks

Often you might be required to send out emails containing particular reports and/or output from commands run.

To do so, you use features from the .NET Framework via PowerShell to send out emails, as shown in here:

Function Send-Mail {
param([string]$To,[string]$From,[string]$Subject, `
[string]$Body,[string]$File,[string]$SmtpServer)
If($SmtpServer -eq ""){
$SmtpServer = "FQDN of your SMTP server here"
}
$Smtp = New-Object System.Net.Mail.SMTPclient($SmtpServer)
$Message = New-Object
System.Net.Mail.MailMessage($From,$To,$Subject,$Body)
If ($File -ne "") {
$Attach = New-Object System.Net.Mail.Attachment $File
$Message.Attachments.Add($Attach)
}
$smtp.Send($message)
}

You can enter the preceding code into a script or directly to the console. If you type the code in the console, you must press the Enter key twice (once to close the function and another time on an empty line) before the PowerShell prompt returns.

In the preceding code listing, functionality from the .NET Framework is used to get SMTP functionality. A function is used so that this code could be easily copied as required into new scripts, and so on. Calling the function is then easy, and passing the command-line arguments is shown here (the PowerShell prompt can vary depending on whether the default PowerShell is used or the new SQL minishell):

PS>Send-Mail -To "[email protected] " -From "[email protected]" –Subject
"Automated Email" -Body "Testing" -File "C:\reports\report.txt"

Note

You might need to configure some antivirus programs to allow the PowerShell.exe process (or sqlps.exe) to “talk” over the SMTP protocol port (TCP 25).


Scheduling Scripts

From time to time, it may be useful to have a method to schedule PowerShell scripts to run automatically based on a particular schedule (when the SQL Server Agent isn’t available locally, for example).

You can easily view the method to call PowerShell scripts by simply typing powershell.exe /? from a PowerShell session, as shown here:

PS>powershell.exe /?
...
PowerShell -Command "& {Get-EventLog -LogName security}"
...

Only a very small section of the text displayed is shown in this example. The powershell.exe can be used for scheduling regular PowerShell scripts. sqlps.exe works similarly, and you can also access its help by passing a slash and question mark (/?) to the command:

PS SQLSERVER:\> sqlps.exe /?


sqlps [ [ [-NoLogo] [-NoExit] [-NoProfile]
[-OutputFormat {Text | XML}] [-InputFormat {Text | XML}]
]
[-Command { -
| <string> [ <command_parameters> ]
| <script_block> [ -args <argument_array> ]
}
]
]
[ -Help | -?]

-NoLogo
Do not display the copyright banner on startup.
-NoExit
Keep running after completing all startup commands.
-NoProfile
Do not load a user profile.
-OutputFormat
Format the output of all objects as either text strings (Text) or in a
serialized CLIXML format (XML).
-InputFormat
The input from stdin is formatted as either text strings (Text) or in a
serialized CLIXML format (XML).
-Command
sqlps runs the commands specified and then exits, unless -NoExit is also
specified. Do not specify other characters after the -Command switch,
they will be read as command arguments.
-
Read input commands from the keyboard by using stdin.
<string> [ <command_parameters> ]
Specifies a string containing the PowerShell commands to be run. Use
the format "&{<command>}". The quotation marks identify a string and
the invocation operator (&) causes sqlps to run the command.
<script_block> [ -args <argument_array> ]
Specifies a block of PowerShell commands to be run. Use the format
{<script_block>}.
-Help | -?
Show the syntax summary help.



Note

How do you know whether to use powershell.exe or sqlps.exe when scheduling jobs? If you’re using anything relating to SMO and/or the SQL cmdlets in the script, sqlps.exe would seem to be easier to use because all the prerequisites to using SMO and the SQL cmdlets are already loaded, which can save several lines in a script. As a reminder, the SQL minishell is limited in its functionality, so powershell.exe may be required in particular if you need to load some PowerShell functionality from another application, such as Exchange.


As discussed briefly earlier, SQL Server Agent can also be used to run scheduled PowerShell commands.

Other -----------------
- PowerShell in SQL Server 2008
- Protecting SQL Server Data : SCHEMA ARCHITECTURE STRATEGIES - Using Database Object Schemas
- Protecting SQL Server Data : SCHEMA ARCHITECTURE STRATEGIES - Protection via Normalization
- Troubleshooting and Optimizing SQL Server 2005 : Server Configuration Maintenance
- Troubleshooting and Optimizing SQL Server 2005 : Tuning the Database Structure
- Troubleshooting and Optimizing SQL Server 2005 : Data Analysis and Problem Diagnosis
- SQL Injection Attacks and Defense : Exploiting the Operating System - Consolidating Access
- SQL Injection Attacks and Defense : Executing Operating System Commands
- Administering SQL Server 2008 with PowerShell : PowerShell Scripting Basics (part 2)
- Administering SQL Server 2008 with PowerShell : PowerShell Scripting Basics (part 1)
- Administering SQL Server 2008 with PowerShell : Overview of PowerShell
- SQL Server 2008 Scheduling and Notification : Scripting Jobs and Alerts, Multiserver Job Management & Event Forwarding
- SQL Server 2008 Scheduling and Notification : Managing Alerts
- SQL Injection Attacks and Defense : Accessing the File System (part 2) - Writing Files
- SQL Injection Attacks and Defense : Accessing the File System (part 1) - Reading Files
- SQL Server 2008 Scheduling and Notification : Managing Jobs
- SQL Server 2008 Scheduling and Notification : Managing Operators
- SQL Server 2008 Scheduling and Notification : Configuring the SQL Server Agent
- SQL Server 2008 : Database Mail - Related Views and Procedures
- SQL Server 2008 : Database Mail - Using SQL Server Agent Mail
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us